package com.chuangyang.fixboxmaster.service;


import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;

import com.android.volley.Response;
import com.chuangyang.fixboxlib.bean.LocationEntity;
import com.chuangyang.fixboxlib.utils.LocationUtils;
import com.chuangyang.fixboxlib.utils.LogUtils;
import com.chuangyang.fixboxmaster.BuildConfig;
import com.chuangyang.fixboxmaster.bean.BaseInfo;

import java.util.HashMap;


public class UploadLocationService extends Service {

    public static final String ACTION = "com.chuangyang.fixboxmaster.service.UploadLocationService";
    public static int DELAY_TIME = 60 * 60 * 1000;
    public static final int TEST_DELAY_TIME = 60 * 60 * 1000; //ms
    private static final String TAG = UploadLocationService.class.getSimpleName();
    private boolean isConnected = true;
    private static int times = 0;
    private LocationUtils locationUtils;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter mConnectFilter = new IntentFilter();
        mConnectFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mReceiver, mConnectFilter);
        locationUtils=new LocationUtils(this);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        if (isConnected) {
            new MonitorThread().start();
        }
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo info = connectivityManager.getActiveNetworkInfo();
                if (info != null && info.isAvailable() && info.isConnected()) {
                    String name = info.getTypeName();
                    LogUtils.LOGI(TAG, "connected,current networkType: " + name);
                    isConnected = true;
                } else {
                    LogUtils.LOGI(TAG, "disconnected");
                    isConnected = false;
                }
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }

    private void updateData() {
        if (isConnected) {
            LogUtils.LOGI(TAG, "begin fech data: " + times);
            times++;
            locationUtils.startLocation(new LocationUtils.LocationListener() {
                @Override
                public void onReceiveLocation(LocationEntity entity) {
                    HashMap<String, String> param = new HashMap<>();
                    param.put("latitude", entity.getLatitude());
                    param.put("longitude",entity.getLongitude());
                    locationUtils.stopLocation();
                    RequestManager.addRequest(new GsonRequest<>(FixBoxApi.GET_UPLOAD_LOCATION, param, BaseInfo.class, responseListener));
                }
            });   //TODO 百度定位重复调用多次

        }
    }

    private Response.Listener<BaseInfo> responseListener=new Response.Listener<BaseInfo>() {
        @Override
        public void onResponse(BaseInfo response) {
            if(BuildConfig.DEBUG){
        //        Toast.makeText(AppData.getContext(), "上传经纬度成功", Toast.LENGTH_LONG).show();
            }
        }
    };


    private class MonitorThread extends Thread {
        @Override
        public void run() {
            super.run();
            updateData();
        }
    }


}
